// pages/health/search/search.js
Page({
  data: {
    keyword: '',
    historyList: ['苹果', '小炒黄牛肉', '米饭'],
    discoverList: ['米饭', '鸡蛋', '玉米', '苹果', '红薯', '香蕉']
  },

  onLoad(options) {
    if (options && options.q) {
      this.setData({ keyword: options.q });
    }
  },

  goBack() { wx.navigateBack(); },

  onInput(e) { this.setData({ keyword: e.detail.value }); },
  onConfirm() { this.onSearch(); },

  tapChip(e) {
    const text = e.currentTarget.dataset.text;
    this.setData({ keyword: text }, () => this.onSearch());
  },

  clearHistory() { this.setData({ historyList: [] }); },

  onSearch() {
    const q = (this.data.keyword || '').trim();
    if (!q) {
      wx.showToast({ title: '请输入关键字', icon: 'none' });
      return;
    }
    const history = this.data.historyList.slice();
    if (history.indexOf(q) === -1) history.unshift(q);
    this.setData({ historyList: history.slice(0, 10) });
    wx.navigateTo({ url: `/pages/health/searchResult/searchResult?q=${encodeURIComponent(q)}` });
  }
})